package com.wswy.wzcx.ui.dialog;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v7.app.AppCompatDialog;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.wswy.wzcx.R;

public class LoadingDialog extends AppCompatDialog {

  public LoadingDialog(Context context,String loadingText) {
    super(context, R.style.customTransparentDialog);
    this.loadingText = loadingText;
  }

  private String loadingText;

  private TextView tvLoading;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dialog_loading);

    ProgressBar progressBar = findViewById(R.id.progressBar);

    tvLoading= findViewById(R.id.tv_loading_status);
    tvLoading.setText(loadingText);

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
      Drawable drawable = progressBar.getIndeterminateDrawable();
      if (drawable != null) {
        Drawable wrapDrawable = DrawableCompat.wrap(drawable);
        DrawableCompat.setTint(wrapDrawable, ContextCompat.getColor(getContext(), R.color.white));
        progressBar.setIndeterminateDrawable(DrawableCompat.unwrap(wrapDrawable));
      }
    }

    setCancelable(false);
    setCanceledOnTouchOutside(false);

  }

  public void startLoading(String loading){
    setOnDismissListener(null);
    if (isShowing()){
      dismiss();
    }
    loadingText = loading;
    if (tvLoading != null){
      tvLoading.setText(loadingText);
    }

    show();
  }

  public void setSuccess(String loadingSuccess){
    if (tvLoading != null && isShowing()) {
      tvLoading.setText(loadingSuccess);

      tvLoading.postDelayed(new Runnable() {
        @Override
        public void run() {
          dismiss();
        }
      }, 1500);
    }
  }


}
